﻿using STT.Layer.Connection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace STT.Profilehv.UserControls
{
    public partial class UcBangDiem : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadInfoHV();
        }
        private void LoadInfoHV()
        {
            dtldiem.DataSource = tb.GetDataTableById("Get_DiemHocVien_Diem_byMahv", Session["Mahv"].ToString());
            dtldiem.DataBind();
            //dtldiem.DataSource = tb.GetDataTableById("GetNamDiemHocVien", "1");
            //dtldiem.DataBind();
        }

        protected void dtldiem_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item)
            {
                //DataTable nam = tb.GetDataTableById("GetNamDiemHocVien", "1");
                //DataRowView drv = e.Item.DataItem as DataRowView;
                //DataList innerDataList = e.Item.FindControl("DataList1") as DataList;
                //innerDataList.DataSource = tb.GetDataTableById("GetNamDiemHocVien", "1");
                //innerDataList.DataBind();

            }
            if (txttimkiem.Text != "")
            {
                DataTable dt = tb.GetDataTableById3("Get_DiemHocVien_Diem_byMahv_NamHoc", Session["Mahv"].ToString(), txttimkiem.Text);
                if (dt.Rows.Count >0)
                {
                    dtldiem.Visible = true;
                    Pnthongbao.Visible = false;
                    if (e.Item.ItemType == ListItemType.Header)
                    {
                        Label lblHead = (Label)e.Item.FindControl("Label10");
                        lblHead.Text = "Năm học" + "\t" + dt.Rows[0][3].ToString();
                    }
                    if (e.Item.ItemType == ListItemType.Footer)
                    {
                        Double tongdiem = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            tongdiem = tongdiem + (Convert.ToDouble(dt.Rows[i][2].ToString()));
                        }
                        Double diemtb = tongdiem / (dt.Rows.Count);
                        Label lbldiemtrungbinh = (Label)e.Item.FindControl("lbldiemtb");
                        lbldiemtrungbinh.Text = "Điểm trung bình : " + Math.Round(diemtb,1, MidpointRounding.AwayFromZero).ToString();
                        Label lblxeploai = (Label)e.Item.FindControl("lblxeploai");
                        if (diemtb > 8) lblxeploai.Text = "Xếp Loại : Giỏi";
                    }
                    
                }
                else
                {
                    dtldiem.Visible = false;
                    dtldiem.DataSource = null;
                    Pnthongbao.Visible = true;
                }
            }
            else
            {
                DataTable dt = tb.GetDataTableById("Get_DiemHocVien_Diem_byMahv", Session["Mahv"].ToString());
                if (dt.Rows.Count >0)
                {
                    dtldiem.Visible = true;
                    Pnthongbao.Visible = false;
                    if (e.Item.ItemType == ListItemType.Header)
                    {
                        Label lblHead = (Label)e.Item.FindControl("Label10");
                        lblHead.Text = "Năm học" + "\t" + dt.Rows[0][3].ToString();
                    }

                    if (e.Item.ItemType == ListItemType.Footer)
                    {
                        Double tongdiem = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            tongdiem = tongdiem + (Convert.ToDouble(dt.Rows[i][2].ToString()));
                        }
                        Double diemtb = tongdiem / (dt.Rows.Count);
                        Label lbldiemtrungbinh = (Label)e.Item.FindControl("lbldiemtb");
                        lbldiemtrungbinh.Text = "Điểm trung bình : " + Math.Round(diemtb, 1, MidpointRounding.AwayFromZero).ToString();
                        Label lblxeploai = (Label)e.Item.FindControl("lblxeploai");
                        if (diemtb > 8) lblxeploai.Text = "Xếp Loại : Giỏi";
                    }
                }
                else
                {
                    dtldiem.Visible = false;
                    Pnthongbao.Visible = true;
                    dtldiem.DataSource = null;
                   
                }
            }
        }

        protected void btntimkiemnam_Click(object sender, EventArgs e)
        {

            DataTable dt = tb.GetDataTableById3("Get_DiemHocVien_Diem_byMahv_NamHoc", Session["Mahv"].ToString(), txttimkiem.Text);
            if (dt.Rows.Count > 0)
            {
                dtldiem.Visible = true;
                dtldiem.DataSource = dt;
                dtldiem.DataBind();
                Pnthongbao.Visible = false;
            }
            else
            {
                dtldiem.Visible = false;
                Pnthongbao.Visible = true;
            }
        }

    }
}